package com.visitorspecials.tankbattle;

import java.util.Hashtable;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.opengl.GLES10;
import android.graphics.*;

/*
 * Hi! Welcome to Ye-olde Graphics class! 
 * This class is managed by Dylan Reynolds, and should not be violated by anyone else
 * Trespassers will be shot
 * those still alive will be shot again
 * those found thinking of trespassing will also be shot.
 * 
 * Get off my damn lawn. Ya Yankee scum.
 */
public class Sprite extends Mesh
{
	private Bitmap _texture;

	private static Hashtable<String, Bitmap> textures;
	
	private float _size;
	
	private String _textureName;

	public static void initTextures(Context context)
	{
		textures = new Hashtable<String, Bitmap>();

		textures.put("test", BitmapFactory.decodeResource(
				context.getResources(), R.drawable.test));
		textures.put("house", BitmapFactory.decodeResource(
				context.getResources(), R.drawable.house));
		textures.put("back", BitmapFactory.decodeResource(
				context.getResources(), R.drawable.back));
		textures.put("bullet", BitmapFactory.decodeResource(
				context.getResources(), R.drawable.bullet));
		textures.put("bush", BitmapFactory.decodeResource(
				context.getResources(), R.drawable.bush));
		textures.put("house", BitmapFactory.decodeResource(
				context.getResources(), R.drawable.house));
		textures.put("rock", BitmapFactory.decodeResource(
				context.getResources(), R.drawable.rock));
		textures.put("tank", BitmapFactory.decodeResource(
				context.getResources(), R.drawable.tank));
		textures.put("turret", BitmapFactory.decodeResource(
				context.getResources(), R.drawable.turret));
		
		
		// Load textures here later.
		/*
		 * textures.put("tank", BitmapFactory.decodeResource(
		 * context.getResources(), R.drawable.tank)); textures.put("turret",
		 * BitmapFactory.decodeResource( context.getResources(),
		 * R.drawable.turret));
		 */
	}

	public float getSize()
	{
		return this._size;
	}
	
	public String getTextureName()
	{
		return this._textureName;
	}
	
	public float getHeading()
	{
		return this._heading;
	}
	
	public Sprite(String texture, float x, float y, float heading, float size, float btmX, float topX, float btmY, float topY)
	{		
		this.x = x;
		this.y = y;
		this._size = size;
		this._heading = heading;
		this._textureName = texture;
		this._texture = textures.get(this._textureName);
		this.setBitmap(this._texture);

		float textureCoordinates[] = { 
				btmX, topY,
				topX, topY,
				btmX, btmY,
                topX, btmY};
		
		//float textureCoordinates[] = { 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
		                  //   1.0f, 0.0f, };


		/*
		 * this is a work in progress implementation of how to set the
		 * vertices of our sprite, before i go further i want to know how the
		 * coordinate system on android and/or opengl works so i'll be drawing
		 * these right. If anyone (Matthew) knows the answer to this, feel
		 * free to complete the implementation
		 * 
		 * 
		 * float vertices[] = new float[] { left, top, left, bottom, right,
		 * bottom, right, top }
		 * 
		 * until a definite solution is found, i'm going to use a modified
		 * version of what can be found in this dude's code:
		 * http://code.google
		 * .com/p/opengl-es-tutorial-for-android/source/browse
		 * /trunk/Tutorial%20
		 * Part%20VI/src/com/jayway/opengl/tutorial/mesh/SimplePlane.java (we
		 * should definetly credit him/legally download his license and all
		 * that, will give more info on usage of his code if you guys ask me)
		 */
		
		/*
		 * This array is a bit of magic.
		 * 
		 * We have four points, that draw our image.  Lets call them A, B, C, D, and quadrilateral ABCD forms our image like so:
		 * 
		 * 
		 * A---------B
		 * |         |
		 * |         |
		 * |         |
		 * D---------C
		 * 
		 * or
		 * 
		 * A----B
		 *  \    \
		 *   \    \
		 *    \    \
		 *     \    \
		 *      D----C
		 * 
		 *  This makes sense, no?
		 *  
		 *  the array vertices[] works as follows:
		 *  
		 *  point = (index x, index y)
		 *  
		 *  These are the points:
		 *  
		 *  A = (4,5)
		 *  B = (6,7)
		 *  C = (2,3)
		 *  D = (0,1)
		 *  
		 *  
		 *  
		 *  The coordinate system works like this, if this is the screen of the device:
		 *  
		 *  (-1,1)---------(1,1)
		 *  | 			   |
		 *  |                  |
		 *  |                  |
		 *  (-1,-1)-------(1,-1)
		 *  
		 *  
		 *  To place it at with the center of the image at (x,y) with width [width], do this:
		 *  w2 = width/2
		 *  A = (x - w2, y + w2)
		 *  B = (x + w2, y + w2)
		 *  C = (x + w2, y - w2)
		 *  D = (x - w2, y - w2) 
		 */
		
		float w2 = size / 2;
		
		float ax = x - w2;
		float bx = x + w2;
		float cx = x + w2;
		float dx = x - w2;
		
		float ay = y + w2;
		float by = y + w2;
		float cy = y - w2;
		float dy = y - w2;
		
		float[] vertices = new float[] {dx, dy, cx, cy, ax, ay, bx, by};
		short[] indices = new short[] { 0, 1, 2, 1, 3, 2 };

		this.setIndices(indices);
		this.setVertices(vertices);
		this.setTextureCoordinates(textureCoordinates);
	}
}
